package com.sahala.downimage.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import com.sahala.downimage.domain.ImageDO;

public class HttpUtils {

	private static Logger logger = Logger.getLogger(HttpUtils.class);

	public static Set<Integer> getListPageId(int pageNum) {
		HttpClient client = new HttpClient();
		String url = "http://www.duitang.com/blogs/tag/hot/"
				+ pageNum
				+ "/?tags=%E6%89%8B%E5%B7%A5%2C%E7%BB%A3%E6%B3%95%2C%E6%89%8B%E4%BD%9C%2C%E6%8A%98%E7%BA%B8%2C%E6%8A%98%E7%BA%B8&_type=";
		GetMethod get = new GetMethod(url);

		Set<Integer> result = new HashSet<Integer>();
		try {
			int statusCode = client.executeMethod(get);
			if (statusCode != 200) {
				logger.error("getListPageId@statusCode is not 200,page:" + url);
				return null;
			} else {
				System.getProperties().setProperty("http.proxyHost", "122.72.0.105 ");
				System.getProperties().setProperty("http.proxyPort", "80");
			}
			String responseContext = get.getResponseBodyAsString();

			if (StringUtils.isBlank(responseContext)) {
				return result;
			}
			JSONObject data = JSONObject.fromObject(responseContext);
			JSONObject blogs = (JSONObject) data.get("data");
			JSONArray array = (JSONArray) blogs.get("blogs");
			for (int i = 0; i < array.size(); i++) {
				JSONObject value = (JSONObject) array.get(i);
				result.add((Integer) value.get("id"));
			}

		} catch (HttpException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			client = null;
		}
		return result;
	}

	public static List<ImageDO> getImageDoList(long pageId) {
		List<ImageDO> result = new ArrayList<ImageDO>();

		Parser parser = new Parser();
		try {
			parser.setURL("http://www.duitang.com/people/mblog/" + pageId
					+ "/detail/");
			parser.setEncoding("utf-8");
			NodeList  nodeList = parser.extractAllNodesThatMatch(new NodeClassFilter(ImageTag.class));
			SimpleNodeIterator iter = nodeList.elements();
			while(iter.hasMoreNodes()){
				ImageTag node = (ImageTag) iter.nextNode();
				String id = node.getAttribute("data-rootid");
				if(null != id){
					String imageUrl = node.getImageURL();
					String[] values = StringUtils.split(imageUrl, "/");
					ImageDO image = new ImageDO();
					image.setImageUrl(imageUrl);
					for (String value : values) {
						if (value.matches("[0-9]{2}")) {
							image.setGenDate(value);
						}
						if (value.matches("[0-9]{6}")) {
							image.setYearMonth(value);
						}
					}
					image.setImgName(values[values.length - 1]);
					result.add(image);
				}
			}
			
		} catch (ParserException e) {
			e.printStackTrace();
		}

	
		return result;
	}

	public static void savePicToDisk(String rootPath, ImageDO image) {
		HttpClient client = new HttpClient();
		GetMethod get = new GetMethod(image.getImageUrl());
		OutputStream out = null;
		InputStream input = null;
		try {
			int statusCode = client.executeMethod(get);
			if (statusCode != 200) {
				logger.error("getImageLink@statusCode is not 200,page:"
						+ image.getImageUrl());
				return;
			}else {
				System.getProperties().setProperty("http.proxyHost", "61.187.64.20");
				System.getProperties().setProperty("http.proxyPort", "80");
			}
			byte bodys[] = get.getResponseBody();
			// input = get.getResponseBodyAsStream();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String filePath = rootPath + File.separator
					+ sdf.format(new Date()) + File.separator;
			File dir = new File(filePath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			out = new FileOutputStream(new File(filePath + image.getImgName()));
			out.write(bodys);
			out.flush();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
				if (input != null)
					input.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

			client = null;
		}

	}

	public static void main(String[] args) {
		List<ImageDO> imageUrls = getImageDoList(34295325);

	//	 savePicToDiskV2("E:\\pic", imageUrls.get(0));

		System.out.println(imageUrls);
		/*
		 * Set<Integer> set = getListPageId(1); System.out.println(set);
		 */
	}


}
